package com.jonah.demo.hbase.thrift;


import com.jonah.demo.hbase.thrift.common.MD5Utils;
import com.jonah.demo.hbase.thrift.config.HBaseThriftConfig;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import java.nio.ByteBuffer;

public class Thrift2Demo {
    public static void main(String[] args) throws TException {
        HBaseThriftConfig instant = HBaseThriftConfig.getInstant();


        String thrift2Host = instant.getThrift2Host();
        Integer thrift2PortInt = instant.getThrift2PortInt();
        String tableName = instant.getTableName();
        Long startRowLong = instant.getStartRowLong();
        Long endRowLong = instant.getEndRowLong();
        TTransport transport = new TSocket(thrift2Host, thrift2PortInt, 5000);
        TProtocol protocol = new TBinaryProtocol(transport);
        THBaseService.Iface client = new THBaseService.Client(protocol);


        for (long index = startRowLong; index < endRowLong; index++) {
            transport.open();
            ByteBuffer table = ByteBuffer.wrap(tableName.getBytes());
            String phone = String.valueOf(index);
            String md5 = MD5Utils.getMD5Code(phone);
            ByteBuffer row = ByteBuffer.wrap(md5.getBytes());
            TGet get = new TGet(table);
            get.setRow(row);
            TResult result = client.get(table, get);
            System.out.println(new String(result.getRow()));
            result.getColumnValues().forEach(c -> {
                System.out.println(new String(result.getRow()) + "  " + new String(c.getFamily()) + "_" + new String(c.getQualifier()) + ":" + new String(c.getValue()));
            });

        }

    }
}
